package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatUserVipPrivilege;
import com.happy.cat.project.system.service.ICatUserVipPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 会员等级特权Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userPrivilege" )
@Api("会员等级特权 API")
public class CatUserVipPrivilegeController extends BaseController {

    private final ICatUserVipPrivilegeService iCatUserVipPrivilegeService;

    /**
     * 查询会员等级特权列表
     * @return
     */
    @ApiOperation(value = "查询会员等级特权列表",notes = "查询会员等级特权列表",response = TableDataInfo.class)
    @GetMapping("/all")
    public AjaxResult all() {
        return AjaxResult.success(iCatUserVipPrivilegeService.list());
    }

    /**
     * 查询会员等级特权列表
     */
    @ApiOperation(value = "查询会员等级特权列表",notes = "查询会员等级特权列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userPrivilege:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatUserVipPrivilege catUserVipPrivilege)
    {
        startPage();
        LambdaQueryWrapper<CatUserVipPrivilege> lqw = new LambdaQueryWrapper<CatUserVipPrivilege>();
        if (StringUtils.isNotBlank(catUserVipPrivilege.getName())){
            lqw.like(CatUserVipPrivilege::getName ,catUserVipPrivilege.getName());
        }
        if (StringUtils.isNotBlank(catUserVipPrivilege.getPrivilegeKey())){
            lqw.eq(CatUserVipPrivilege::getPrivilegeKey ,catUserVipPrivilege.getPrivilegeKey());
        }
        if (StringUtils.isNotBlank(catUserVipPrivilege.getPrivilegeImg())){
            lqw.eq(CatUserVipPrivilege::getPrivilegeImg ,catUserVipPrivilege.getPrivilegeImg());
        }
        if (StringUtils.isNotBlank(catUserVipPrivilege.getRemarks())){
            lqw.eq(CatUserVipPrivilege::getRemarks ,catUserVipPrivilege.getRemarks());
        }
        if (catUserVipPrivilege.getCreateDate() != null){
            lqw.eq(CatUserVipPrivilege::getCreateDate ,catUserVipPrivilege.getCreateDate());
        }
        if (catUserVipPrivilege.getStatus() != null){
            lqw.eq(CatUserVipPrivilege::getStatus ,catUserVipPrivilege.getStatus());
        }
        List<CatUserVipPrivilege> list = iCatUserVipPrivilegeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出会员等级特权列表
     */
    @ApiOperation(value = "导出会员等级特权列表",notes = "导出会员等级特权列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPrivilege:export')" )
    @Log(title = "会员等级特权" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatUserVipPrivilege catUserVipPrivilege) {
        LambdaQueryWrapper<CatUserVipPrivilege> lqw = new LambdaQueryWrapper<CatUserVipPrivilege>(catUserVipPrivilege);
        List<CatUserVipPrivilege> list = iCatUserVipPrivilegeService.list(lqw);
        ExcelUtil<CatUserVipPrivilege> util = new ExcelUtil<CatUserVipPrivilege>(CatUserVipPrivilege. class);
        return util.exportExcel(list, "userPrivilege" );
    }

    /**
     * 获取会员等级特权详细信息
     */
    @ApiOperation(value = "获取会员等级特权详细信息",notes = "获取会员等级特权详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPrivilege:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatUserVipPrivilegeService.getById(id));
    }

    /**
     * 新增会员等级特权
     */
    @ApiOperation(value = "新增会员等级特权",notes = "新增会员等级特权",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPrivilege:add')" )
    @Log(title = "会员等级特权" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatUserVipPrivilege catUserVipPrivilege) {
        catUserVipPrivilege.setCreateDate(new Date());
        catUserVipPrivilege.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatUserVipPrivilegeService.save(catUserVipPrivilege) ? 1 : 0);
    }

    /**
     * 修改会员等级特权
     */
    @ApiOperation(value = "修改会员等级特权",notes = "修改会员等级特权",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPrivilege:edit')" )
    @Log(title = "会员等级特权" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatUserVipPrivilege catUserVipPrivilege) {
        catUserVipPrivilege.setCreateDate(new Date());
        catUserVipPrivilege.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatUserVipPrivilegeService.updateById(catUserVipPrivilege) ? 1 : 0);
    }

    /**
     * 删除会员等级特权
     */
    @ApiOperation(value = "删除会员等级特权",notes = "删除会员等级特权",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userPrivilege:remove')" )
    @Log(title = "会员等级特权" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatUserVipPrivilegeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
